﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CSharpStudy.依赖注入
{
    class DI_3
    {
        //public static void Main(string[] args)
        //{
        //    //IFactory factory = FactoryContext.Factory;
        //    //var window = factory.MakeWindow();
        //    //Console.WriteLine(window.ShowInfo());
        //    //Console.ReadLine();

        //    //反射工厂--低活
        //    var bt = ReflectionFactory.MakeWindow();
        //    Console.WriteLine(bt.ShowInfo());
        //    Console.ReadLine();
        //}
    }

    internal static class ReflectionFactory
    {
        private static string _WindowType;
        private static string _ButtonType;
        private static string _TextType;

        static ReflectionFactory()
        {
            XmlDocument xml = new XmlDocument();
            xml.Load("../../依赖注入/di2.xml");
            var XmlNodes = xml.ChildNodes[1];
            _WindowType = XmlNodes.ChildNodes[0].ChildNodes[0].Value;
            _ButtonType = XmlNodes.ChildNodes[1].ChildNodes[0].Value;
            _TextType = XmlNodes.ChildNodes[2].ChildNodes[0].Value;
        }

        public static IWindow MakeWindow()
        {
            return Assembly.Load("CSharpStudy").CreateInstance("CSharpStudy.依赖注入." + _WindowType) as IWindow;
        }

        public static IButton MakeButton()
        {
            return Assembly.Load("CSharpStudy").CreateInstance("CSharpStudy.依赖注入." + _ButtonType) as IButton;
        }

        public static ITextBox MakeTextBox()
        {
            return Assembly.Load("CSharpStudy").CreateInstance("CSharpStudy.依赖注入." + _TextType) as ITextBox;
        }
    }

    /// <summary>
    /// windows窗体，实现
    /// </summary>
    class WindowsWindow:IWindow
    {
        public string WindowName { get; private set; }

        public WindowsWindow()
        {
            WindowName = "windows风格";
           
        }

        public string ShowInfo()
        {
            return WindowName;
        }
    }

    /// <summary>
    /// mac窗体，实现
    /// </summary>
    class MacWindow:IWindow
    {

        public string WindowName { get; private set; }

        public MacWindow()
        {
            WindowName = "Mac风格";
        }

        public string ShowInfo()
        {
            return WindowName;
        }
    }

    /// <summary>
    /// 工厂上下文类，管理
    /// </summary>
   static class FactoryContext
    {
        public static IFactory Factory{get; private set; }

       static FactoryContext()
       {
           
           XmlDocument xml=new XmlDocument();
           xml.Load("../../依赖注入/di.xml");
           string type = xml.ChildNodes[1].ChildNodes[0].ChildNodes[0].Value;
           switch (type)
           {
               case "Windows":
                   Factory=new WindowsFactory();
                   break;
               case "Mac":
                   Factory=new MacFactory();
                   break;
               default :
                   return;
           }
       }

    }
    /// <summary>
    /// windows工厂类，实现
    /// </summary>
    class WindowsFactory:IFactory
    {

        public IWindow MakeWindow()
        {
            return new WindowsWindow();
        }

        public IButton MakeButton()
        {
            throw new NotImplementedException();
        }

        public ITextBox MakeTextBox()
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// mac工厂类，实现
    /// </summary>
    class MacFactory:IFactory
    {

        public IWindow MakeWindow()
        {
            return new MacWindow();
        }

        public IButton MakeButton()
        {
            throw new NotImplementedException();
        }

        public ITextBox MakeTextBox()
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// 工厂接口
    /// </summary>
    interface IFactory
    {
        /// <summary>
        /// 窗体
        /// </summary>
        /// <returns></returns>
        IWindow MakeWindow();
        /// <summary>
        /// 按钮
        /// </summary>
        /// <returns></returns>
        IButton MakeButton();
        /// <summary>
        /// 文本框
        /// </summary>
        /// <returns></returns>
        ITextBox MakeTextBox();
    }
    /// <summary>
    /// window接口
    /// </summary>
    interface IWindow
    {
        string ShowInfo();
    }
    /// <summary>
    /// button接口
    /// </summary>
    interface IButton
    {
        string ShowInfo();
    }
    /// <summary>
    /// textbox接口
    /// </summary>
    interface ITextBox
    {
        string ShowInfo();
    }
}
